<?php
declare (strict_types=1);

namespace app\exam\service\admin;

use app\common\service\BaseService;
use app\exam\logic\admin\ModuleRollupPaper as ModuleRollupPaperLogic;
use app\exam\logic\admin\Paper as PaperLogic;
use app\exam\logic\admin\PaperCategory;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;
use think\Response;

/**
 * Class ModuleRollupPaper
 *
 * @package app\exam\service\admin
 */
class ModuleRollupPaper
{
    /**
     * @Inject()
     *
     * @var ModuleRollupPaperLogic
     */
    protected $moduleRollupPaperLogic;

    /**
     * @Inject()
     *
     * @var PaperCategory
     */
    protected $paperCategoryLogic;

    /**
     * @Inject()
     *
     * @var PaperLogic
     */
    protected $paperLogic;

    /**
     * 套卷试卷列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/3/2
     *
     * @param int $rollupId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function paperList(int $rollupId)
    {
        $map      = ['rollup_id' => $rollupId];
        $paperMap = [];
        // 关键字
        Request::has('title', 'get', true) && $paperMap[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // 试卷类型
        if (Request::has('paper_type', 'get', true) && Request::get('paper_type/d', -1) !== -1) {
            $paperMap[] = ['paper_type', '=', Request::get('paper_type/d')];
        }
        // 分类
        $category = Request::get('category/d', 0);
        $category && $paperMap[] = ['cate', '=', $category];

        $data = $this->moduleRollupPaperLogic->getList($map, $paperMap);

        // 定义字段
        $keyList   = [
            'title'           => ListItem::make()->title('试卷名称'),
            'paper_type_text' => ListItem::make()->title('试卷类型'),
            'cate_title'      => ListItem::make()->title('试卷分类'),
            'topic_count'     => ListItem::make()->title('题数'),
        ];
        $category  = $this->paperCategoryLogic->cascader();
        $paperType = $this->paperLogic->getPaperTypeOptions();
        // 搜索字段
        $searchKey = [
            'paper_type' => FormItem::make('select')->title(false)->placeholder('试卷类型')->options($paperType),
            // 'category'   => FormItem::make('select')->title(false)->placeholder('试卷分类')->options($category),
            'title'      => FormItem::make()->title(false)->placeholder('试卷名称')->maxlength(30),
        ];

        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->datas     = $data;
        $list->searchKey = $searchKey;

        return $list->send();
    }

    /**
     * 获取选项面板配置数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getChooseOptions()
    {
        $category  = $this->paperCategoryLogic->cascader();
        $paperType = $this->paperLogic->getPaperTypeOptions();

        return BaseService::parseToData([
            'options' => [
                'category'   => $category,
                'paper_type' => $paperType,
            ]
        ], 1);
    }

    /**
     * 获取试卷列表
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/4
     *
     * @param int $rollupId
     * @return \eduline\admin\page\interfaces\think\Response
     */
    public function getChooseList(int $rollupId)
    {
        $map = [];
        // 关键字
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        // 试卷类型
        if (Request::has('paper_type', 'get', true) && Request::get('paper_type/d', -1) !== -1) {
            $map[] = ['paper_type', '=', Request::get('paper_type/d')];
        }

        // 难易度
        $difficulty = Request::get('difficulty/d', 0);
        $difficulty && $map[] = ['point', '=', $difficulty];
        $data = $this->moduleRollupPaperLogic->getChooseList($rollupId, $map);
        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->datas = $data;

        return $list->send();
    }

    /**
     * 新建试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/1/31
     *
     * @return Response
     */
    public function create(int $rollupId)
    {
        $data = Request::post('papers/a');
        $this->moduleRollupPaperLogic->createModuleRollupPaper($rollupId, $data);

        return BaseService::parseToData([], 1, '保存成功');
    }

    /**
     * 移除试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @param int    $rollupId
     * @param string $id
     * @return \think\Response
     */
    public function delete(int $rollupId, string $id)
    {
        $this->moduleRollupPaperLogic->deleteModuleRollupPaper($rollupId, $id);

        return BaseService::parseToData([], 1, '移除成功');
    }

    /**
     * 批量移除试卷
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/3
     *
     * @param int    $rollupId
     * @param string $ids
     * @return \think\Response
     */
    public function deletes(int $rollupId, string $ids)
    {
        $this->moduleRollupPaperLogic->deleteModuleRollupPaper($rollupId, $ids);

        return BaseService::parseToData([], 1, '批量移除成功');
    }
}